#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>

void init_gl_context();
void framebuffer_size_cb(GLFWwindow *, int, int);
void setup_shader_program();
void setup_RC();
void run(GLFWwindow *);
void processInput(GLFWwindow *window);

const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
const char *vertexShaderSource = "#version 330 core\n"
                                 "layout (location = 0) in vec3 aPos;\n"
                                 "void main()\n"
                                 "{\n"
                                 "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
                                 "}\0";
const char *fragmentShaderSource = "#version 330 core\n"
                                   "out vec4 FragColor;\n"
                                   "void main()\n"
                                   "{\n"
                                   "   FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
                                   "}\n\0";

GLFWwindow *glfw_window;
unsigned int shaderProgram;
unsigned int vao;
unsigned int vbo;

int main(int, char **)
{
    init_gl_context();
    if (glfw_window == NULL)
    {
        return -1;
    }

    setup_shader_program();
    setup_RC();

    run(glfw_window);

    glDeleteVertexArrays(1, &vao);
    glDeleteBuffers(1, &vbo);
    glDeleteProgram(shaderProgram);

    glfwTerminate();

    return 0;
}

// OpenGL context include GLFW and GLAD
void init_gl_context()
{
    // glfw: initialize and configure
    // ------------------------------
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    glfw_window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Hellow Triangle!", NULL, NULL);
    if (glfw_window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return;
    }

    glfwMakeContextCurrent(glfw_window);
    glfwSetFramebufferSizeCallback(glfw_window, framebuffer_size_cb);

    //glad: load all OpenGL function pointers
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed  to initialize GLAD" << std::endl;
        glfwTerminate();
        return;
    }
}

void framebuffer_size_cb(GLFWwindow *window, int w, int h)
{
    glViewport(0, 0, w, h);
}

void processInput(GLFWwindow *window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

void setup_shader_program()
{
    unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
    unsigned int frameShader = glCreateShader(GL_FRAGMENT_SHADER);
    shaderProgram = glCreateProgram();
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);
    glShaderSource(frameShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(frameShader);
    // link
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, frameShader);
    glLinkProgram(shaderProgram);
}

void setup_RC()
{
    float triangle[] = {
        -0.5, -0.5, 0,
        0.5, -0.5, 0,
        0, 0.5, 0};
    glGenVertexArrays(1, &vao);
    glGenBuffers(1, &vbo);
    //prepare data
    glBindVertexArray(vao);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(triangle), triangle, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
    glEnableVertexAttribArray(0);
    glBindVertexArray(0);
}

void run(GLFWwindow *window)
{
    while (!glfwWindowShouldClose(window))
    {
        // input
        // -----
        processInput(window);

        //render
        // ----
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // draw triangle
        glUseProgram(shaderProgram);
        glBindVertexArray(vao);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }
}